﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using Tasker.Composition;
using Tasker.Infrastructure.Repositories.Base;
using Tasker.Repositories.Base;
using Tasker.Repositories.Entities;


namespace Tasker.Repositories.Resolvers
{
    /// <summary>
    /// Resolver for infrastructure services.
    /// </summary>
    public class InfrastructureResolver : IDependencyResolver,
        IDependencyTypeResolver<DbConnection>,
        IDependencyTypeResolver<EntitiesContext>,
        //IDependencyTypeResolver<ITransactionManager>,
        IDependencyTypeResolver<IContextManager>
        //IDependencyTypeResolver<IConnectionManager>
    {
        DbConnection IDependencyTypeResolver<DbConnection>.Resolve()
        {
            return new SqlConnection(ConfigurationManager.ConnectionStrings["Tasker"].ConnectionString);
        }

        EntitiesContext IDependencyTypeResolver<EntitiesContext>.Resolve()
        {
            MetadataWorkspace m = new MetadataWorkspace(new string[] { "res://*/Entities.EntitiesModel.csdl", "res://*/Entities.EntitiesModel.ssdl", "res://*/Entities.EntitiesModel.msl" }, new Assembly[] { Assembly.GetExecutingAssembly() });
            EntityConnection con = new EntityConnection(m, Resolver.Resolve<DbConnection>());
            return new EntitiesContext(con);
        }

        //ITransactionManager IDependencyTypeResolver<ITransactionManager>.Resolve() { return new TransactionScopeTransactionManager(); }
        IContextManager IDependencyTypeResolver<IContextManager>.Resolve() { return new EFContextManager(); }
        //IConnectionManager IDependencyTypeResolver<IConnectionManager>.Resolve() { return new SimpleConnectionManager(); }
    }
}
